package todo_plan

import (
	"github.com/gin-gonic/gin"
	"log"
	"strconv"
	"tao-admin/configs"
	"tao-admin/internal/model"
)

type PlanManager struct{}

type SearchPlanCondition struct {
	Title       *string `json:"title"`
	Description *string `json:"description"`
	Type        *string `json:"type"`
	Tags        *string `json:"tags"`
	Priority    *string `json:"priority"`
	Status      *string `json:"status"`
	Target      *string `json:"target"`
	Stime       *int64  `json:"stime"`
	Etime       *int64  `json:"etime"`
}

func (manager *PlanManager) SearchPlanByConditionMng(ctx *gin.Context, condition *SearchPlanCondition) ([]model.ToDoPlanTab, int64, error) {
	page, _ := strconv.Atoi(ctx.Query("page"))
	count, _ := strconv.Atoi(ctx.Query("perPage"))

	db := configs.GetGormDB()
	list := make([]model.ToDoPlanTab, 0)
	var total int64 = 0

	if condition.Title != nil {
		db = db.Where("title=?", condition.Title)
	}
	if condition.Description != nil {
		db = db.Where("description=?", condition.Description)
	}
	if condition.Tags != nil {
		db = db.Where("tags=?", condition.Tags)
	}
	if condition.Priority != nil {
		db = db.Where("priority=?", condition.Priority)
	}
	if condition.Type != nil {
		db = db.Where("type=?", condition.Type)
	}
	if condition.Status != nil {
		db = db.Where("status=?", condition.Status)
	}
	if condition.Stime != nil {
		db = db.Where("stime=?", condition.Stime)
	}
	if condition.Etime != nil {
		db = db.Where("etime=?", condition.Etime)
	}

	err1 := db.Model(&model.ToDoPlanTab{}).Count(&total).Error
	if page > 0 && count > 0 {
		db = db.Limit(count).Offset((page - 1) * count)
	}

	//db = db.Order("priority DESC, status ASC")
	db = db.Order("id DESC, priority DESC, status ASC")

	err2 := db.Find(&list).Error
	if err1 != nil || err2 != nil {
		return nil, 0, nil
	}

	log.Printf("查表结束, list: %v", list)
	return list, total, nil
}

func (manager *PlanManager) CreatePlanMng(ctx *gin.Context, item model.ToDoPlanTab) error {
	db := configs.GetGormDB()
	err := db.Create(&item).Error
	if err != nil {
		return err
	}
	return nil
}

func (manager *PlanManager) UpdatePlanMng(ctx *gin.Context, item model.ToDoPlanTab) error {
	db := configs.GetGormDB()
	// 先赋值
	updateData := model.ToDoPlanTab{
		Title:       item.Title,
		Description: item.Description,
		Type:        item.Type,
		Tags:        item.Tags,
		Priority:    item.Priority,
		Status:      item.Status,
		Target:      item.Target,
		Stime:       item.Stime,
		Etime:       item.Etime,
	}

	id := item.Id
	// 查询记录
	if err := db.Where("id = ?", id).First(&item).Error; err != nil {
		return err
	}

	err := db.Model(&item).Where("id = ?", id).Update(updateData).Error
	if err != nil {
		return err
	}
	return nil
}

func (manager *PlanManager) DeletePlanMng(ctx *gin.Context, item model.ToDoPlanTab) error {
	db := configs.GetGormDB()
	id := item.Id
	// 查询记录
	if err := db.Where("id = ?", id).First(&item).Error; err != nil {
		return err
	}
	err := db.Delete(&item).Error
	if err != nil {
		return err
	}
	return nil
}
